package com.example.practice2023.service;

import com.example.practice2023.mapper.MemberMapper;
import com.example.practice2023.pojo.Member;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MemberService {

    @Resource
    private MemberMapper memberMapper;

    public Map<String,Object> selectBymemberId(Integer memberId){
        Member member = memberMapper.selectBymemberId(memberId);

        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("data", member);

        return map;
    }

    public Map<String,Object> insertMember(Member member){
        Map<String, Object> map = new HashMap<>();
        String MemberPass = member.getMemberPass();
        // 这里手动加密5次 MD5
        String md5Pass = DigestUtils.md5DigestAsHex(MemberPass.getBytes(StandardCharsets.UTF_8));
        md5Pass = DigestUtils.md5DigestAsHex(md5Pass.getBytes(StandardCharsets.UTF_8));
        md5Pass = DigestUtils.md5DigestAsHex(md5Pass.getBytes(StandardCharsets.UTF_8));
        md5Pass = DigestUtils.md5DigestAsHex(md5Pass.getBytes(StandardCharsets.UTF_8));
        md5Pass = DigestUtils.md5DigestAsHex(md5Pass.getBytes(StandardCharsets.UTF_8));
        member.setMemberPass(md5Pass);
        member.setMemberCreateTime(new Timestamp(System.currentTimeMillis()));
        memberMapper.insertMember(member);
        map.put("code", 200);
        map.put("msg", "新增会员成功");
        map.put("data", member);
        return map;

    }
    public Map<String,Object> updateBymemberId(Member member){
        Map<String, Object> map = new HashMap<>();
        String MemberPass = member.getMemberPass();
        // 这里手动加密5次 MD5
        String md5Pass = DigestUtils.md5DigestAsHex(MemberPass.getBytes(StandardCharsets.UTF_8));
        md5Pass = DigestUtils.md5DigestAsHex(md5Pass.getBytes(StandardCharsets.UTF_8));
        md5Pass = DigestUtils.md5DigestAsHex(md5Pass.getBytes(StandardCharsets.UTF_8));
        md5Pass = DigestUtils.md5DigestAsHex(md5Pass.getBytes(StandardCharsets.UTF_8));
        md5Pass = DigestUtils.md5DigestAsHex(md5Pass.getBytes(StandardCharsets.UTF_8));
        member.setMemberPass(md5Pass);
        member.setMemberCreateTime(new Timestamp(System.currentTimeMillis()));
        memberMapper.updateBymemberId(member);
        map.put("code", 200);
        map.put("msg", "更新会员成功");
        map.put("data", member);
        return map;

    }
    public Map<String,Object> deleteBymemberId(Integer memberId){
        Map<String, Object> map = new HashMap<>();
        memberMapper.deleteBymemberId(memberId);
        map.put("code", 200);
        map.put("msg", "更新会员成功");
        map.put("data", memberId);
        return map;

    }

    public Map<String, Object> selectMemberByPage(int page, int limit,Member member) {
        int index = (page - 1) * limit;   // 通用性的分页起始下标计算
        List<Member> memberList = memberMapper.selectMemberByPage(index, limit,member.getMemberId(),member.getMemberPhone(),member.getMemberName(),member.getGrade(),member.getMemberStatus());
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count",memberMapper.selectAllMemberCount(member.getMemberId(),member.getMemberPhone(),member.getMemberName(),member.getGrade(),member.getMemberStatus()));
        map.put("data", memberList);
        return map;
    }

    public Map<String,Object> updateSwitch(Member member){
        Map<String, Object> map = new HashMap<>();
        memberMapper.updateSwitch(member);
        map.put("code", 200);
        map.put("msg", "修改状态成功");
        map.put("data", member);
        return map;

    }
}
